package com.fingard.dsp.bank.directbank.abc02;

import com.fingard.client.DspClient;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.FtpSet;
import com.fingard.dsp.bank.directConfig.NodeSalaryItem;
import com.fingard.dsp.bank.directConfig.SalaryConfig;
import com.fingard.dsp.bank.directbank.PaymentState;
import com.fingard.dsp.bank.directbank.abc02.util.RsaHelper;
import com.fingard.dsp.bank.directbank.bankInterface.IBatPayRec;
import com.fingard.dsp.bank.helpers.FileType;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.ReqQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.RetAtsQryBatPayRec;
import com.fingard.dsp.bank.reqrespobj.batpayrec.detail.RetAtsBatPayRecDetail;
import com.fingard.dsp.salaryset.SalaryDecrypt;
import com.fingard.io.FileHelper;
import com.fingard.net.ConnState;
import com.fingard.net.FtpPortClient;
import com.fingard.net.FtpReceiver;
import com.fingard.net.TcpReceiver;
import com.fingard.text.MFSHelper;
import com.fingard.text.StringHelper;
import com.fingard.util.FtpUtil;
import com.fingard.util.RSAEncryptHelper;
import com.fingard.util.SmbFileUtil;
import com.fingard.util.secret.salary.DESUtil;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;
import com.xwbank.sdk.base64.BASE64Decoder;
import com.xwbank.sdk.base64.BASE64Encoder;
import org.springframework.stereotype.Component;

import java.io.File;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.*;

import static com.fingard.text.StringHelper.trimEndFileSp;

/**
 * 农行直联批量代收付
 *
 * @author Cruise
 * @date 2019/4/19
 */
@Component("ABC02BatPayRec")
public class ABC02BatPayRec extends ABC02Base implements IBatPayRec {
    @Override
    public RetAtsBatPayRec d_1988_BatPay(ReqBatPayRec p_req1988) throws Exception {
        String enableSalaryDecrypt = getBankConfig().getValueAsString("enableSalaryDecrypt");
        String commedTypeArrs = getBankConfig().getValueAsString("salaryDecryptCommand");
        if (("true".equals(enableSalaryDecrypt)) && (StringHelper.hasAnyChar(commedTypeArrs))) {
            String[] arrs = commedTypeArrs.split(",");
            List<String> list = Arrays.asList(arrs);
            if (list.contains(p_req1988.reqHeader.commandCode)) {
                if (StringHelper.isNullOrEmpty(p_req1988.decryptKey)) {
                    throw new Exception("获取密钥失败");
                }
                p_req1988.decryptKey = RSAEncryptHelper.base64PlusRSADecryptAndUncompress(p_req1988.decryptKey, "utf-8", SalaryDecrypt.rsaPrivateKey);
                p_req1988.isNeedDecrypt = true;
                this.isNeedHiddleField = Boolean.valueOf(true);
            }
            if (StringHelper.hasAnyChar(p_req1988.decryptKey)) {
                String key = p_req1988.reqHeader.bankCode + "-" + p_req1988.reqHeader.corporation + "-" + p_req1988.reqHeader.group;
                SalaryConfig salaryConfig = SalaryDecrypt.getSalaryConfig(key);
                if (salaryConfig == null) {
                    throw new Exception("获取渠道：" + key + " 的解密配置失败");
                }
                Map<String, NodeSalaryItem> map = salaryConfig.nodeSalaryItemMap;
                p_req1988.salaryItemMap = map;
                if (map.keySet().contains("TotalAmount")) {
                    NodeSalaryItem item = map.get("TotalAmount");
                    String totalAmt = DESUtil.jdkDESDecryptMode(p_req1988.totalAmt, p_req1988.decryptKey);
                    if (StringHelper.hasAnyChar(totalAmt)) {
                        p_req1988.totalAmt = totalAmt.split(item.union)[(item.index - 1)];
                    }
                }
            }
        }
        return d_BatPayRec(p_req1988);
    }

    @Override
    public RetAtsBatPayRec d_9188_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return d_BatPayRec(p_req9188);
    }

    public RetAtsBatPayRec d_9288_BatPay(ReqBatPayRec p_req9188) throws Exception {
        return d_BatPayRec_9288(p_req9188);
    }

    @Override
    public RetAtsQryBatPayRec d_1998_QryBatPay(ReqQryBatPayRec p_req1998) throws Exception {
        return d_QryBatPayRec(p_req1998);
    }

    @Override
    public RetAtsQryBatPayRec d_9198_QryBatPay(ReqQryBatPayRec p_req9198) throws Exception {
        return d_QryBatPayRec(p_req9198);
    }

    public RetAtsBatPayRec d_BatDeductionOfDifferenceRec(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        try {
            ABC02Header bankHeader = new ABC02Header();

            //构造差额代扣文件
            StringBuilder tmpSbData = new StringBuilder();
            int index = 0;
            String splitStr = "|";
            while (p_reqBat.readRecord()) {
                tmpSbData.append(index++).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.recName).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.recAct).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.recCur).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.payAmount).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.memo).append(splitStr).append("\r\n");
            }
            //构造请求报文
            bankHeader.CCTransCode = "CFRB19";
            bankHeader.ReqSeqNo = p_reqBat.reqBatchNO;
            bankHeader.ReqDateTime = p_reqBat.getPayDateTime();
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_reqBat.ownAct));
            xmlWriter.writeElementString("DbProv", p_reqBat.ownArea);
            xmlWriter.writeElementString("DbCur", this.convertCurToBank(p_reqBat.ownCur));
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("DbAccName", p_reqBat.readedDetail.recName);
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Cme");
            xmlWriter.writeElementString("InterBusFlag", "9");
            xmlWriter.writeEndElement();
            xmlWriter.writeElementString("Amt", p_reqBat.totalAmt);
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("SumNum", p_reqBat.totalNum);
            xmlWriter.writeEndElement();
            xmlWriter.writeElementString("FileFlag", "1");
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("BatchFileName", p_reqBat.reqBatchNO + ".txt");
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("SplitStr", "|");
            xmlWriter.writeElementString("Postscript", p_reqBat.cnapscode);
            xmlWriter.writeElementString("NVoucherType", "");
            xmlWriter.writeElementString("NFAccNo", "");
            xmlWriter.writeEndElement();

            xmlWriter.writeEndElement();
            tmpSbData.delete(tmpSbData.length() - 2, tmpSbData.length());
            String tmpLocalMarkPath = getTempFileDirectory() + File.separator + p_reqBat.reqBatchNO + ".txt";
            WriteBankLogLn2("批次代扣明细序号映射文件路径: " + tmpLocalMarkPath);
            FileHelper.writeFile(tmpLocalMarkPath, tmpSbData);
            String tmpFilePath = "";
            if (StringHelper.isNullOrEmpty(bankFront.bkRecFolder)) {
                bankFront.bkRecFolder = bankFront.bkPayFolder;
            }
            tmpFilePath = StringHelper.trimEnd(bankFront.bkPayFolder, File.separator) + File.separator + tmpSbData;

//			WriteBankLogLn("发薪文件的保存地址：" + tmpFilePath+"plain");
            String cipherData = "";
            if ("dspfileserver".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                WriteBankLogLn("accessBkFileMode：" + bankFront.accessBkFileMode);
                DspClient dspClient = new DspClient(bankFront.dspFileServer);
                dspClient.charsetName = getCharset();
                // 华润发薪专用
                if (isNeedHiddleField) {
//					FileHelper.writeFile(tmpFilePath+"plain", tmpSbData.toString(), getCharset());
                    dspClient.charsetName = getCharset();
                    //获取客户私钥文件路径
                    String bankPubKeyPath = getBankConfig().getValueAsString("BankPubKeyPath");
                    PublicKey publicKey = RsaHelper.decodePublicKeyFromFile(bankPubKeyPath);
                    //对文件内容进行加密
                    byte[] encryptedDate = RsaHelper.encryptData(tmpSbData.toString().getBytes("gbk"), publicKey);
                    cipherData = new BASE64Encoder().encode(encryptedDate);
                }
                TcpReceiver retReceiver = dspClient.putFile(isNeedHiddleField ? cipherData : tmpSbData.toString(), FileType.bankFile, tmpFilePath, false);
                if (retReceiver.isCompleted) {
                    String retXml = new String(retReceiver.recBytes, retReceiver.contentStartIndex, retReceiver.contentLength, retReceiver.charsetName);
                    XmlTextReader retXmlRd = new XmlTextReader(retXml);
                    if ("0".equals(retXmlRd.getSimpleText("RespCode"))) {
                        WriteBankLogLn("文件保存至前置机" + tmpFilePath + "成功");
                    } else {
                        WriteBankLogLn("文件保存至前置机" + tmpFilePath + "失败，响应报文：" + retXml);
                    }
                } else {
                    WriteBankLogLn("文件" + tmpFilePath + "保存失败：" + retReceiver.getConnMsg());
                    if (retReceiver.connState.equals(ConnState.FAIL)) {
                        WriteBankLogLn("连接主DSP服务" + bankFront.dspFileServer + "失败");
                    }
                }
            } else {
                WriteBankLogLn("请求代收付文件内容为:" + tmpSbData.toString());
                WriteBankLogLn("文件传送地址为:" + tmpFilePath);
                if (isNeedHiddleField) {

                    String bankPubKeyPath = getBankConfig().getValueAsString("BankPubKeyPath");
                    PublicKey publicKey = RsaHelper.decodePublicKeyFromFile(bankPubKeyPath);
                    //对文件内容进行加密
                    byte[] encryptedDate = RsaHelper.encryptData(tmpSbData.toString().getBytes("gbk"), publicKey);
                    cipherData = new BASE64Encoder().encode(encryptedDate);
                    FileHelper.writeFile(tmpFilePath, cipherData, getCharset());
                } else {
                    FileHelper.writeFile(tmpFilePath, tmpSbData.toString(), getCharset());
                }
            }
            xmlWriter.writeEndDocument();// 写入报文完成
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");

                    //当RxtInfo有内容返回，交易就会有异常
                    if (StringHelper.hasAnyChar(tmpRetRxtInfo)) {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpRetRxtInfo;
                    } else {
                        retAts.respCode = "0";
                        retAts.reqSeqID = p_reqBat.reqBatchNO;
                        retAts.transState = tmpRespSource + "+" + tmpRespCode;
                        retAts.payInfoCode = tmpRespCode;
                        retAts.respInfo = tmpRetInfo;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = tmpStrRet[0];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            WriteBankLogLn(e);
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqBat.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    public RetAtsBatPayRec d_BatPayRec_9288(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        try {
            ABC02Header bankHeader = new ABC02Header();
            //构造差额代扣文件
            StringBuilder tmpSbData = new StringBuilder();
            int index = 0;
            String splitStr = "|";
            while (p_reqBat.readRecord()) {
                tmpSbData.append(p_reqBat.readedDetail.askNumber).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.recName).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.recAct).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.recCur).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.payAmount).append(splitStr);
                tmpSbData.append(p_reqBat.readedDetail.transInfo).append(splitStr).append("\r\n");
            }
            //构造请求报文
            bankHeader.CCTransCode = "CFRB19";
            bankHeader.ReqSeqNo = p_reqBat.reqBatchNO;
            bankHeader.ReqDateTime = p_reqBat.getPayDateTime();
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_reqBat.ownAct));
            xmlWriter.writeElementString("DbProv", p_reqBat.ownArea);
            xmlWriter.writeElementString("DbCur", this.convertCurToBank(p_reqBat.ownCur));
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("DbAccName", p_reqBat.ownActName);
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Cme");
            xmlWriter.writeElementString("InterBusFlag", "9");
            xmlWriter.writeEndElement();
            xmlWriter.writeElementString("Amt", p_reqBat.totalAmt);
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("SumNum", p_reqBat.totalNum);
            xmlWriter.writeEndElement();
            xmlWriter.writeElementString("FileFlag", "1");
            xmlWriter.writeStartElement("Cmp");
            xmlWriter.writeElementString("BatchFileName", p_reqBat.reqBatchNO + ".txt");
            xmlWriter.writeEndElement();
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("SplitStr", "|");
            xmlWriter.writeElementString("Postscript", p_reqBat.useType);
            xmlWriter.writeElementString("NVoucherType", "");
            xmlWriter.writeElementString("NFAccNo", "");
            xmlWriter.writeEndElement();

            //1.ERP先将批量文件上传到ICT的detail目录，然后发起请求报文
            StringBuilder tmpSbSeqIDData = new StringBuilder();
            tmpSbData.delete(tmpSbData.length() - 2, tmpSbData.length());
            String tmpLocalMarkPath = getTempFileDirectory() + File.separator + p_reqBat.reqBatchNO + ".txt";
            WriteBankLogLn2("批次明细序号映射文件路径: " + tmpLocalMarkPath);
            FileHelper.writeFile(tmpLocalMarkPath, tmpSbSeqIDData);
            String tmpFilePath = "";
            String tmpDataFileName = p_reqBat.reqBatchNO + ".txt";
            if (StringHelper.isNullOrEmpty(bankFront.bkRecFolder)) {
                bankFront.bkRecFolder = bankFront.bkPayFolder;
            }
            tmpFilePath = StringHelper.trimEnd(bankFront.bkRecFolder, File.separator) + File.separator + tmpDataFileName;

//			WriteBankLogLn("发薪文件的保存地址：" + tmpFilePath+"plain");
            String cipherData = "";
            if ("dspfileserver".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                WriteBankLogLn("accessBkFileMode：" + bankFront.accessBkFileMode);
                DspClient dspClient = new DspClient(bankFront.dspFileServer);
                dspClient.charsetName = getCharset();
                TcpReceiver retReceiver = dspClient.putFile(isNeedHiddleField ? cipherData : tmpSbData.toString(), FileType.bankFile, tmpFilePath, false);
                if (retReceiver.isCompleted) {
                    String retXml = new String(retReceiver.recBytes, retReceiver.contentStartIndex, retReceiver.contentLength, retReceiver.charsetName);
                    XmlTextReader retXmlRd = new XmlTextReader(retXml);
                    if ("0".equals(retXmlRd.getSimpleText("RespCode"))) {
                        WriteBankLogLn("文件保存至前置机" + tmpFilePath + "成功");
                    } else {
                        WriteBankLogLn("文件保存至前置机" + tmpFilePath + "失败，响应报文：" + retXml);
                    }
                } else {
                    WriteBankLogLn("文件" + tmpFilePath + "保存失败：" + retReceiver.getConnMsg());
                    if (retReceiver.connState.equals(ConnState.FAIL)) {
                        WriteBankLogLn("连接主DSP服务" + bankFront.dspFileServer + "失败");
                    }
                }
            } else if ("smb".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                WriteBankLogLn("请求代收付文件内容为:" + tmpSbData.toString());
                //上传文件到前置机目录
                if (FileHelper.existFile(tmpLocalMarkPath)) {
                    String remotePath = StringHelper.trimEnd(bankFront.bkRecFolder, File.separator) + "/";
                    WriteBankLogLn("文件上传共享目录地址为:" + remotePath);
                    int result = SmbFileUtil.smbUploadingInChar(tmpLocalMarkPath, "", remotePath);
                    if (result == -1) {
                        retAts.respCode = "-1";
                        retAts.respInfo = "上传文件到前置机目录异常";
                        retAts.writeRetToATS();
                        return retAts;
                    } else if (result == 1) {
                        retAts.respCode = "-1";
                        retAts.respInfo = "文件已存在共享目录无需重复上传";
                        retAts.writeRetToATS();
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "本地批付文件不存在";
                    retAts.writeRetToATS();
                    return retAts;
                }
            } else {
                WriteBankLogLn("请求代收付文件内容为:" + tmpSbData.toString());
                WriteBankLogLn("文件传送地址为:" + tmpFilePath);
                if (isNeedHiddleField) {

                    String bankPubKeyPath = getBankConfig().getValueAsString("BankPubKeyPath");
                    PublicKey publicKey = RsaHelper.decodePublicKeyFromFile(bankPubKeyPath);
                    //对文件内容进行加密
                    byte[] encryptedDate = RsaHelper.encryptData(tmpSbData.toString().getBytes("gbk"), publicKey);
                    cipherData = new BASE64Encoder().encode(encryptedDate);
                    FileHelper.writeFile(tmpFilePath, cipherData, getCharset());
                } else {
                    FileHelper.writeFile(tmpFilePath, tmpSbData.toString(), getCharset());
                }
            }
            xmlWriter.writeEndDocument();// 写入报文完成
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");

                    //当RxtInfo有内容返回，交易就会有异常
                    if (StringHelper.hasAnyChar(tmpRetRxtInfo)) {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpRetRxtInfo;
                    } else {
                        retAts.respCode = "0";
                        retAts.reqSeqID = p_reqBat.reqBatchNO;
                        retAts.transState = tmpRespSource + "+" + tmpRespCode;
                        retAts.payInfoCode = tmpRespCode;
                        retAts.respInfo = tmpRetInfo;
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = tmpStrRet[0];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            WriteBankLogLn(e);
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqBat.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }


    /**
     * 批量代收付提交
     *
     * @param p_reqBat
     * @return
     * @throws Exception
     */
    public RetAtsBatPayRec d_BatPayRec(ReqBatPayRec p_reqBat) throws Exception {
        RetAtsBatPayRec retAts = new RetAtsBatPayRec(p_reqBat.reqHeader);
        try {
            ABC02Header bankHeader = new ABC02Header();

            if ("198802s".equals(p_reqBat.reqHeader.commandCode)) {
                if (p_reqBat.isSameBnk()) {
                    p_reqBat.reqHeader.commandCode = "1988CFRT03";
                } else {
                    p_reqBat.reqHeader.commandCode = "1988IBBF23";
                }
            }
            if (p_reqBat.reqHeader.transType.equals("1988")) {
                bankHeader.CCTransCode = "IBAF04";
                // 他行支付
                if ("1988IBBF23".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "IBBF23";
                }
                if ("198801N".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "IBAF04";
                }
                if ("1988CFRT05".equals(p_reqBat.reqHeader.commandCode)) {
                    // 费用报销
                    bankHeader.CCTransCode = "CFRT05";
                }
                if ("1988CFRT03".equals(p_reqBat.reqHeader.commandCode) || "198801".equals(p_reqBat.reqHeader.commandCode)) {
                    // 代发工资
                    bankHeader.CCTransCode = "CFRT03";
                }
                if ("1988IBBF01".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "IBBF01";
                }
                if ("1988IBBF02".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "IBBF02";
                }
                if ("198802".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "CFRB17";
                }
            } else if (p_reqBat.reqHeader.transType.equals("9188")) {
                bankHeader.CCTransCode = "IBAF05";
                if ("918804".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "CFRB16";
                } else if ("918803".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "CFRA05";
                } else if ("9188C410".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "C410";
                } else if ("9188IBAF16".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "IBAF16";
                } else if ("9188IBAF17".equals(p_reqBat.reqHeader.commandCode)) {
                    bankHeader.CCTransCode = "IBAF17";
                }
            }

            bankHeader.ReqSeqNo = p_reqBat.reqBatchNO;
            bankHeader.ReqDateTime = p_reqBat.getPayDateTime();
            XmlTextWriter xmlWriter = new XmlTextWriter();
            writeCommonHeader(xmlWriter, bankHeader);
            xmlWriter.writeElementString("Amt", p_reqBat.totalAmt);
            if (!"1988CFRT05".equals(p_reqBat.reqHeader.commandCode)) {
                xmlWriter.writeElementString("FileFlag", "1");
            }
            String tmpDataFileName;
            if ("198801".equals(p_reqBat.reqHeader.commandCode) || "1988CFRT03".equals(p_reqBat.reqHeader.commandCode)) {
                tmpDataFileName = p_reqBat.reqBatchNO + "." + bankFront.org + "_" + bankFront.operator + "." + Format.Date8Format.format(new Date()) + ".ffs";
            } else {
                tmpDataFileName = p_reqBat.reqBatchNO + ".txt";
            }
            // Cmp
            if ("198801".equals(p_reqBat.reqHeader.commandCode) || "918803".equals(p_reqBat.reqHeader.commandCode) || "1988CFRT03".equals(p_reqBat.reqHeader.commandCode)) {
                xmlWriter.writeStartElement("Cmp");
                xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_reqBat.ownAct));
                xmlWriter.writeElementString("DbProv", p_reqBat.ownArea);
                xmlWriter.writeElementString("DbCur", this.convertCurToBank(p_reqBat.ownCur));
                xmlWriter.writeElementString("BatchFileName", tmpDataFileName);
                if (isNeedHiddleField) {
                    xmlWriter.writeElementString("FileEncryptFlag", "1");
                } else {
                    xmlWriter.writeElementString("FileEncryptFlag", "");
                }
                xmlWriter.writeEndElement();
            } else if ("1988CFRT05".equals(p_reqBat.reqHeader.commandCode)) {
                xmlWriter.writeStartElement("Cmp");
                xmlWriter.writeElementString("DbProv", p_reqBat.ownArea);
                xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_reqBat.ownAct));
                xmlWriter.writeElementString("DbCur", this.convertCurToBank(p_reqBat.ownCur));
                String NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_reqBat.ownAct);
                String NFProv = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, "NFProv");
                String NFCur = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, "NFCur");
                xmlWriter.writeElementString("CrProv", NFProv);
                xmlWriter.writeElementString("CrAccNo", NFAccNo);
                xmlWriter.writeElementString("CrCur", NFCur);
                xmlWriter.writeElementString("BatchFileName", tmpDataFileName);
                xmlWriter.writeEndElement();
            } else {
                xmlWriter.writeStartElement("Cmp");
                xmlWriter.writeElementString("DbAccNo", getAbcAccNo(p_reqBat.ownAct));
                boolean isPrivateArea = getBankConfig().getValueAsBool("isPrivateArea");//是否启用广东省农行区域配置
                if (isPrivateArea) {
                    //启用
                    String ownArea = p_reqBat.ownAct.substring(0, 2);
                    xmlWriter.writeElementString("DbProv", ownArea);
                } else {
                    //不启用
                    xmlWriter.writeElementString("DbProv", p_reqBat.ownArea);
                }

                xmlWriter.writeElementString("DbCur", this.convertCurToBank(p_reqBat.ownCur));
                if (!"9188C410".equals(p_reqBat.reqHeader.commandCode)) {
                    String dbLogAccNo = getBankConfig().getMapValueByKey("dbLogAccNo" + bankHeader.CCTransCode, p_reqBat.useType);
                    if (StringHelper.hasAnyChar(dbLogAccNo)) {
                        xmlWriter.writeElementString("DbLogAccNo", dbLogAccNo);
                    } else {
                        xmlWriter.writeElementClosed("DbLogAccNo");
                    }
                    xmlWriter.writeElementString("SumNum", p_reqBat.totalNum);
                    if (isNeedHiddleField) {
                        xmlWriter.writeElementString("FileEncryptFlag", "1");
                    } else {
                        xmlWriter.writeElementString("FileEncryptFlag", "");
                    }
                }
                xmlWriter.writeElementString("BatchFileName", tmpDataFileName);
                xmlWriter.writeEndElement();
            }
            // Corp
            xmlWriter.writeStartElement("Corp");
            if (!"918804".equals(p_reqBat.reqHeader.commandCode) && !"198802".equals(p_reqBat.reqHeader.commandCode)) {
                if (this.getBankConfig().getValueAsBool("useMemoToPostscript")) {
                    p_reqBat.abstractATS = p_reqBat.memo;
                }
                xmlWriter.writeElementString("Postscript", p_reqBat.abstractATS);
            }
            xmlWriter.writeElementString("DbAccName", p_reqBat.ownActName);
            // 新增CFRA05
            if ("918803".equals(p_reqBat.reqHeader.commandCode) || "9188C410".equals(p_reqBat.reqHeader.commandCode)) {
                xmlWriter.writeElementClosed("BookingDate");
                xmlWriter.writeElementClosed("BookingTime");
                xmlWriter.writeElementString("BookingFlag", "0");
                xmlWriter.writeElementString("TotalNum", p_reqBat.totalNum);
            } else if ("9188IBAF16".equals(p_reqBat.reqHeader.commandCode)) {
                String NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_reqBat.ownAct);
                String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, p_reqBat.ownAct);
                xmlWriter.writeElementString("NVoucherType", NVoucherType);
                xmlWriter.writeElementString("NFAccNo", NFAccNo);
            } else if (!"1988CFRT05".equals(p_reqBat.reqHeader.commandCode) && !"1988CFRT03".equals(p_reqBat.reqHeader.commandCode) && !"198801".equals(p_reqBat.reqHeader.commandCode) && !"9188C410".equals(p_reqBat.reqHeader.commandCode)) {
                if (!isNeedHiddleField) {
                    String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, p_reqBat.useType);
                    xmlWriter.writeElementString("NVoucherType", NVoucherType);
                    String NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_reqBat.ownAct);
                    if (!StringHelper.hasAnyChar(NFAccNo)) {
                        NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_reqBat.ownAct + p_reqBat.useType);
                    }
                    xmlWriter.writeElementString("NFAccNo", NFAccNo);
                } else {
                    String NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_reqBat.ownAct);
                    if (!StringHelper.hasAnyChar(NFAccNo)) {
                        NFAccNo = getBankConfig().getMapValueByKey("nFAccNo" + bankHeader.CCTransCode, p_reqBat.ownAct + p_reqBat.useType);
                    }
                    String NVoucherType = getBankConfig().getMapValueByKey("nVoucherType" + bankHeader.CCTransCode, NFAccNo);
                    xmlWriter.writeElementString("NVoucherType", NVoucherType);
                    xmlWriter.writeElementString("NFAccNo", NFAccNo);
                }
            }
            xmlWriter.writeEndElement();
            if ("918803".equals(p_reqBat.reqHeader.commandCode)) {
                xmlWriter.writeStartElement("Cme");
                xmlWriter.writeElementString("InterBusFlag", "3");
                xmlWriter.writeEndElement();
            } else if ("1988CFRT05".equals(p_reqBat.reqHeader.commandCode)) {
                xmlWriter.writeStartElement("Cme");
                xmlWriter.writeElementString("InterBusFlag", "2");
                xmlWriter.writeEndElement();
            } else if ("1988CFRT03".equals(p_reqBat.reqHeader.commandCode) || "198801".equals(p_reqBat.reqHeader.commandCode)) {
                xmlWriter.writeStartElement("Cme");
                xmlWriter.writeElementString("InterBusFlag", "1");
                xmlWriter.writeEndElement();
            }
            if ("1988CFRT05".equals(p_reqBat.reqHeader.commandCode) || "1988CFRT03".equals(p_reqBat.reqHeader.commandCode) || "198801".equals(p_reqBat.reqHeader.commandCode)) {
                xmlWriter.writeStartElement("Tulip");
                xmlWriter.writeStartElement("批处理");
                xmlWriter.writeElementString("批交易总金额", p_reqBat.totalAmt);
                xmlWriter.writeElementString("批交易总笔数", p_reqBat.totalNum);
                xmlWriter.writeEndElement();
                xmlWriter.writeEndElement();
            }

            StringBuilder tmpSbData = new StringBuilder();
            StringBuilder tmpSbSeqIDData = new StringBuilder();

            int tmpCount = 0;
            String tmpCountSeq = "";
            String transTimeSeq = Format.DateTime14Format.format(new Date());
            while (p_reqBat.readRecord()) {
                if ("1988CFRT05".equals(p_reqBat.reqHeader.commandCode) || "1988CFRT03".equals(p_reqBat.reqHeader.commandCode) || "198801".equals(p_reqBat.reqHeader.commandCode)) {
                    if (tmpSbSeqIDData.length() > 0) {
                        tmpSbSeqIDData.append("\r\n");
                    }
                    tmpSbSeqIDData.append(p_reqBat.readedDetail.askNumber);

                    // 姓名60
                    int spaceLength = 60 - p_reqBat.readedDetail.recName.getBytes("GBK").length;
                    String spaces = "";
                    if (spaceLength >= 0) {
                        spaces = String.format("%" + spaceLength + "s", " ");
                        tmpSbData.append(p_reqBat.readedDetail.recName).append(spaces);
                    } else {
                        StringBuilder newName = new StringBuilder(p_reqBat.readedDetail.recName);
                        while (newName.toString().getBytes("GBK").length > 60) {
                            newName.substring(0, newName.length() - 1);
                        }
                        tmpSbData.append(newName);
                    }
                    // 客户账号30
                    spaceLength = 30 - p_reqBat.readedDetail.recAct.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.recAct).append(spaces);
                    // 交易金额N18.2
                    spaceLength = 18 - p_reqBat.readedDetail.payAmount.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.payAmount).append(spaces);
                    // 摘要C30
                    spaceLength = 30 - p_reqBat.readedDetail.askNumber.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.askNumber).append(spaces);
                    tmpSbData.append("\r\n");
                    continue;
                }

                tmpCount++;
                if ("1988IBBF01".equals(p_reqBat.reqHeader.commandCode) || "1988IBBF02".equals(p_reqBat.reqHeader.commandCode)) {
                    if (tmpSbSeqIDData.length() > 0) {
                        tmpSbSeqIDData.append("\r\n");
                    }
                    tmpSbSeqIDData.append(tmpCount + "," + p_reqBat.readedDetail.askNumber);

                    tmpSbData.append(tmpCount + "|_|");
                    tmpSbData.append(p_reqBat.readedDetail.payAct).append("|_|");
                    tmpSbData.append("|_|");
                    tmpSbData.append(p_reqBat.readedDetail.payName).append("|_|");
                    tmpSbData.append(p_reqBat.readedDetail.recAct + "|_|");
                    if ("1988IBBF02".equals(p_reqBat.reqHeader.commandCode)) {
                        tmpSbData.append("|_|");
                    }
                    tmpSbData.append(p_reqBat.readedDetail.recName + "|_|");
                    tmpSbData.append(this.convertCurToBank(p_reqBat.readedDetail.payCur) + "|_|");
                    tmpSbData.append(p_reqBat.readedDetail.payAmount).append("|_|");
                    tmpSbData.append(p_reqBat.readedDetail.askNumber).append("|_|");

                    if ("1988IBBF01".equals(p_reqBat.reqHeader.commandCode)) {
                        if ("1".equals(p_reqBat.readedDetail.paySpd)) {
                            tmpSbData.append("1|_|");
                        } else {
                            tmpSbData.append("0|_|");
                        }
                        tmpSbData.append(p_reqBat.readedDetail.cnapsName + "|_|");
                        tmpSbData.append(p_reqBat.readedDetail.cnapsCode + "|_|");
                    } else {
                        if ("1".equals(p_reqBat.readedDetail.isPrivate)) {
                            tmpSbData.append("1|_|");
                        } else {
                            tmpSbData.append("0|_|");
                        }
                    }
                    tmpSbData.append("\r\n");
                    continue;
                }

                // 新增918803 CFRA05
                tmpCountSeq = String.format("%07d", tmpCount);
                if ("198802".equals(p_reqBat.reqHeader.commandCode)) {
                    if (tmpSbSeqIDData.length() > 0) {
                        tmpSbSeqIDData.append("\r\n");
                    }
                    tmpSbSeqIDData.append(p_reqBat.readedDetail.askNumber + "," + tmpCountSeq);
                    // 客户编号7
                    tmpSbData.append(tmpCountSeq + "|_|");
                    // 客户姓名70
                    tmpSbData.append(p_reqBat.readedDetail.recName).append("|_|");
                    // 客户账号30
                    if ("1".equals(p_reqBat.readedDetail.isPrivate)) {
                        p_reqBat.readedDetail.recAct = getAbcAccNoWithCode(p_reqBat.readedDetail.recAct, p_reqBat.readedDetail.recArea);
                    }
                    tmpSbData.append(p_reqBat.readedDetail.recAct).append("|_|");
                    //币种3
                    tmpSbData.append("01|_|");
                    // 收款方账簿号
                    tmpSbData.append("|_|");
                    // 交易金额N18.2
                    tmpSbData.append(p_reqBat.readedDetail.payAmount).append("|_|");
                    // 摘要C30
                    tmpSbData.append(p_reqBat.readedDetail.askNumber).append("|_|");
                    // 合约校验标志 01-强制校验 02-均不校验
                    tmpSbData.append("02").append("|_|");
                    // 户名校验标志0-否 1-是
                    tmpSbData.append("0").append("|_|");
                    //客户移动电话校验标识
                    tmpSbData.append("0").append("|_|");
                    //移动电话
                    tmpSbData.append("|_|");
                    //注册证件类型代码
                    tmpSbData.append("|_|");
                    //证件号码
                    tmpSbData.append(p_reqBat.readedDetail.certNum).append("|_|");
                    tmpSbData.append("\r\n");
                    continue;
                }
                if ("9188C410".equals(p_reqBat.reqHeader.commandCode)) {
                    if (tmpSbSeqIDData.length() > 0) {
                        tmpSbSeqIDData.append("\r\n");
                    }
                    tmpSbSeqIDData.append(p_reqBat.readedDetail.askNumber + "," + tmpCountSeq);
                    // 客户编号C20
                    int spaceLength = 20 - p_reqBat.readedDetail.askNumber.getBytes("GBK").length;
                    String spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.askNumber).append(spaces);
                    // 客户姓名C32
                    spaceLength = 32 - p_reqBat.readedDetail.payName.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    if (spaceLength >= 0) {
                        tmpSbData.append(p_reqBat.readedDetail.payName).append(spaces);
                    } else {
                        StringBuilder newName = new StringBuilder(p_reqBat.readedDetail.payName);
                        while (newName.toString().getBytes("GBK").length > 32) {
                            newName.substring(0, newName.length() - 1);
                        }
                        tmpSbData.append(newName);
                    }
                    // 客户账号C30
                    if ("1".equalsIgnoreCase(p_reqBat.readedDetail.isPrivate)) {
                        p_reqBat.readedDetail.payAct = getAbcAccNoWithCode(p_reqBat.readedDetail.payAct, p_reqBat.readedDetail.payArea);
                    }
                    spaceLength = 30 - p_reqBat.readedDetail.payAct.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.payAct).append(spaces);
                    // 交易金额N18.2
                    spaceLength = 18 - p_reqBat.readedDetail.payAmount.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.payAmount).append(spaces);
                    // 摘要C30
                    spaceLength = 30 - p_reqBat.readedDetail.askNumber.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.askNumber).append(spaces);
                    tmpSbData.append("\r\n");
                    continue;
                }
                if ("918803".equals(p_reqBat.reqHeader.commandCode)) {
                    tmpCountSeq = transTimeSeq + tmpCountSeq.substring(1, 7);
                    if (tmpSbSeqIDData.length() > 0) {
                        tmpSbSeqIDData.append("\r\n");
                    }
                    tmpSbSeqIDData.append(tmpCountSeq + "," + p_reqBat.readedDetail.askNumber);
                    // 客户编号20
                    tmpSbData.append(tmpCountSeq);
                    // 客户姓名150
                    int spaceLength = 150 - p_reqBat.readedDetail.payName.getBytes("GBK").length;
                    String spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.payName).append(spaces);
                    // 客户账号30
                    if ("1".equals(p_reqBat.readedDetail.isPrivate)) {
                        p_reqBat.readedDetail.payAct = getAbcAccNoWithCode(p_reqBat.readedDetail.payAct, p_reqBat.readedDetail.payArea);
                    }
                    spaceLength = 30 - p_reqBat.readedDetail.payAct.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.payAct).append(spaces);
                    // 交易金额N18.2
                    spaceLength = 18 - p_reqBat.readedDetail.payAmount.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.payAmount).append(spaces);
                    // 摘要C30
                    spaceLength = 30 - p_reqBat.readedDetail.askNumber.getBytes("GBK").length;
                    spaces = String.format("%" + spaceLength + "s", " ");
                    tmpSbData.append(p_reqBat.readedDetail.askNumber).append(spaces);
                    tmpSbData.append("\r\n");
                    continue;
                }
                if ("918804".equals(p_reqBat.reqHeader.commandCode)) {
                    if (tmpSbSeqIDData.length() > 0) {
                        tmpSbSeqIDData.append("\r\n");
                    }
                    tmpSbSeqIDData.append(p_reqBat.readedDetail.askNumber + "," + tmpCountSeq);
                    // 客户编号7
                    tmpSbData.append(tmpCountSeq + "|_|");
                    // 客户姓名70
                    tmpSbData.append(p_reqBat.readedDetail.payName).append("|_|");
                    // 客户账号30
                    if ("1".equals(p_reqBat.readedDetail.isPrivate)) {
                        p_reqBat.readedDetail.payAct = getAbcAccNoWithCode(p_reqBat.readedDetail.payAct, p_reqBat.readedDetail.payArea);
                    }
                    tmpSbData.append(p_reqBat.readedDetail.payAct).append("|_|");
                    tmpSbData.append(this.convertCurToBank(p_reqBat.readedDetail.payCur) + "|_|");
                    tmpSbData.append("|_|");
                    // 交易金额N18.2
                    tmpSbData.append(p_reqBat.readedDetail.payAmount).append("|_|");
                    // 摘要C30
                    tmpSbData.append(p_reqBat.readedDetail.askNumber).append("|_|");
                    tmpSbData.append("|_|");
                    tmpSbData.append("110001").append("|_|");
                    tmpSbData.append(p_reqBat.readedDetail.certNum).append("|_|");
                    tmpSbData.append("\r\n");
                    continue;
                }
                tmpSbData.append(tmpCountSeq + "|_|");
                if (tmpSbSeqIDData.length() > 0) {
                    tmpSbSeqIDData.append("\r\n");
                }
                tmpSbSeqIDData.append(tmpCountSeq + "," + p_reqBat.readedDetail.askNumber);
                if (p_reqBat.reqHeader.transType.equals("1988")) {
                    if ("1988IBBF23".equals(p_reqBat.reqHeader.commandCode)) {
                        tmpSbData.append(p_reqBat.readedDetail.recAct + "|_|");
                        tmpSbData.append(p_reqBat.readedDetail.recName + "|_|");
                    } else {
                        tmpSbData.append(p_reqBat.readedDetail.recName + "|_|");
                        tmpSbData.append(p_reqBat.readedDetail.recAct + "|_|");
                    }
                    tmpSbData.append(this.convertCurToBank(p_reqBat.readedDetail.payCur) + "|_|");
                } else if (p_reqBat.reqHeader.transType.equals("9188")) {
                    tmpSbData.append(p_reqBat.readedDetail.payName + "|_|");
                    tmpSbData.append(p_reqBat.readedDetail.payAct + "|_|");
                    tmpSbData.append(this.convertCurToBank(p_reqBat.readedDetail.recCur) + "|_|");
                }
                if (!"1988IBBF23".equals(p_reqBat.reqHeader.commandCode)) {
                    tmpSbData.append("|_|");
                }
                tmpSbData.append(p_reqBat.readedDetail.payAmount + "|_|");
                tmpSbData.append(p_reqBat.readedDetail.askNumber + "|_|");
                if ("198801N".equals(p_reqBat.reqHeader.commandCode)) {
                    tmpSbData.append("02|_|");
                    tmpSbData.append("1|_|");
                    tmpSbData.append("0|_|");
                    tmpSbData.append("|_|");
                    tmpSbData.append("|_|");
                    tmpSbData.append("|_|\r\n");
                    continue;
                }
                if ("1988IBBF23".equals(p_reqBat.reqHeader.commandCode)) {
                    if ("1".equals(p_reqBat.readedDetail.paySpd)) {
                        tmpSbData.append("1|_|");
                    } else {
                        tmpSbData.append("0|_|");
                    }
                    tmpSbData.append(p_reqBat.readedDetail.cnapsName + "|_|");
                    tmpSbData.append(p_reqBat.readedDetail.cnapsCode + "|_|" + "\r\n");
                } else {
                    if ("9188IBAF17".equalsIgnoreCase(p_reqBat.reqHeader.commandCode)) {
                        tmpSbData.append("|_|");
                        tmpSbData.append("110001").append("|_|");
                        tmpSbData.append(p_reqBat.readedDetail.certNum).append("|_|");
                        tmpSbData.append("\r\n");
                    } else {
                        if (!"918804".equals(p_reqBat.reqHeader.commandCode)) {
                            tmpSbData.append("02|_|");
                            tmpSbData.append("1|_|");
                            tmpSbData.append("0|_|");
                            tmpSbData.append("|_|");
                        }
                        tmpSbData.append("|_|");
                        tmpSbData.append("|_|\r\n");
                    }
                }
            }
            tmpSbData.delete(tmpSbData.length() - 2, tmpSbData.length());
            String tmpLocalMarkPath = getTempFileDirectory() + File.separator + p_reqBat.reqBatchNO + ".txt";
            WriteBankLogLn2("批次明细序号映射文件路径: " + tmpLocalMarkPath);
            FileHelper.writeFile(tmpLocalMarkPath, tmpSbSeqIDData);

            String tmpLocalFailPath = getTempFileDirectory() + File.separator + Format.Time6Format.format(new Date()) + p_reqBat.reqBatchNO + ".txt";
            WriteBankLogLn2("批次明细上传文件本地路径: " + tmpLocalFailPath);
            FileHelper.writeFile(tmpLocalFailPath, tmpSbData);
            String tmpFilePath = "";
            if (StringHelper.isNullOrEmpty(bankFront.bkRecFolder)) {
                bankFront.bkRecFolder = bankFront.bkPayFolder;
            }
            if (p_reqBat.reqHeader.transType.equals("1988")) {
                tmpFilePath = StringHelper.trimEnd(bankFront.bkPayFolder, File.separator) + File.separator + tmpDataFileName;
            } else if (p_reqBat.reqHeader.transType.equals("9188")) {
                tmpFilePath = StringHelper.trimEnd(bankFront.bkRecFolder, File.separator) + File.separator + tmpDataFileName;
            }
//			WriteBankLogLn("发薪文件的保存地址：" + tmpFilePath+"plain");
            String cipherData = "";
            if ("dspfileserver".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                WriteBankLogLn("accessBkFileMode：" + bankFront.accessBkFileMode);
                DspClient dspClient = new DspClient(bankFront.dspFileServer);
                dspClient.charsetName = getCharset();
                // 华润发薪专用
                if (isNeedHiddleField) {
//					FileHelper.writeFile(tmpFilePath+"plain", tmpSbData.toString(), getCharset());
                    dspClient.charsetName = getCharset();
                    //获取客户私钥文件路径
                    String bankPubKeyPath = getBankConfig().getValueAsString("BankPubKeyPath");
                    PublicKey publicKey = RsaHelper.decodePublicKeyFromFile(bankPubKeyPath);
                    //对文件内容进行加密
                    byte[] encryptedDate = RsaHelper.encryptData(tmpSbData.toString().getBytes("gbk"), publicKey);
                    cipherData = new BASE64Encoder().encode(encryptedDate);
                }
                TcpReceiver retReceiver = dspClient.putFile(isNeedHiddleField ? cipherData : tmpSbData.toString(), FileType.bankFile, tmpFilePath, false);
                if (retReceiver.isCompleted) {
                    String retXml = new String(retReceiver.recBytes, retReceiver.contentStartIndex, retReceiver.contentLength, retReceiver.charsetName);
                    XmlTextReader retXmlRd = new XmlTextReader(retXml);
                    if ("0".equals(retXmlRd.getSimpleText("RespCode"))) {
                        WriteBankLogLn("文件保存至前置机" + tmpFilePath + "成功");
                    } else {
                        WriteBankLogLn("文件保存至前置机" + tmpFilePath + "失败，响应报文：" + retXml);
                    }
                } else {
                    WriteBankLogLn("文件" + tmpFilePath + "保存失败：" + retReceiver.getConnMsg());
                    if (retReceiver.connState.equals(ConnState.FAIL)) {
                        WriteBankLogLn("连接主DSP服务" + bankFront.dspFileServer + "失败");
                    }
                }
            } else if ("ftp".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                if (isNeedHiddleField) {
                    String bankPubKeyPath = getBankConfig().getValueAsString("BankPubKeyPath");
                    PublicKey publicKey = RsaHelper.decodePublicKeyFromFile(bankPubKeyPath);
                    //对文件内容进行加密
                    byte[] encryptedDate = RsaHelper.encryptData(tmpSbData.toString().getBytes("gbk"), publicKey);
                    cipherData = new BASE64Encoder().encode(encryptedDate);
                    FileHelper.writeFile(tmpFilePath, cipherData, getCharset());
                } else {
                    FileHelper.writeFile(tmpFilePath, tmpSbData.toString(), getCharset());
                }
                WriteBankLogLn(p_reqBat.reqBatchNO + "批号代收文件处理完毕,一共" + p_reqBat.getTotalNum() + "条明细.");
                WriteBankLogLn("开始上传文件至FTP...");
                FtpSet ftpSet = bankFront.getFtpSet("batPay");
                String ftpFilePath = StringHelper.trimEndFileSp(ftpSet.upDir) + "/" + tmpDataFileName;
                int result = FtpUtil.ftpUploading(ftpSet, tmpFilePath, ftpFilePath);
                if (result == 0 || result == 1) {
                    WriteBankLogLn("上传批量付款文件成功...");
                } else {
                    WriteBankLogLn("上传批量付款文件失败...");
                    retAts.respCode = "-1";
                    retAts.respInfo = "上传批量付款文件失败...";
                    return retAts;
                }
            } else if ("smb".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                WriteBankLogLn("请求代收付文件内容为:" + tmpSbData.toString());
                //上传文件到前置机目录
                if (FileHelper.existFile(tmpLocalFailPath)) {
                    String remotePath = StringHelper.trimEnd(bankFront.bkRecFolder, File.separator) + "/";
                    WriteBankLogLn("文件上传共享目录地址为:" + remotePath);
                    int result = SmbFileUtil.smbUploadingInChar(tmpLocalFailPath, "", remotePath);
                    if (result == -1) {
                        retAts.respCode = "-1";
                        retAts.respInfo = "上传文件到前置机目录异常";
                        retAts.writeRetToATS();
                        return retAts;
                    } else if (result == 1) {
                        retAts.respCode = "-1";
                        retAts.respInfo = "文件已存在共享目录无需重复上传";
                        retAts.writeRetToATS();
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = "本地批付文件不存在";
                    retAts.writeRetToATS();
                    return retAts;
                }
            } else {
                WriteBankLogLn("请求代收付文件内容为:" + tmpSbData.toString());
                WriteBankLogLn("文件上传地址为:" + tmpFilePath);
                if (isNeedHiddleField) {

                    String bankPubKeyPath = getBankConfig().getValueAsString("BankPubKeyPath");
                    PublicKey publicKey = RsaHelper.decodePublicKeyFromFile(bankPubKeyPath);
                    //对文件内容进行加密
                    byte[] encryptedDate = RsaHelper.encryptData(tmpSbData.toString().getBytes("gbk"), publicKey);
                    cipherData = new BASE64Encoder().encode(encryptedDate);
                    FileHelper.writeFile(tmpFilePath, cipherData, getCharset());
                } else {
                    FileHelper.writeFile(tmpFilePath, tmpSbData.toString(), getCharset());
                }
            }
            xmlWriter.writeEndDocument();// 写入报文完成
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString());// 发送银行
            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                // 解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");

                    //当RxtInfo有内容返回，交易就会有异常
                    if (StringHelper.hasAnyChar(tmpRetRxtInfo)) {
                        retAts.respCode = "-1";
                        retAts.respInfo = tmpRetRxtInfo;
                    } else {
                        retAts.respCode = "0";
                        retAts.reqSeqID = p_reqBat.reqBatchNO;
                        retAts.transState = tmpRespSource + "+" + tmpRespCode;
                        retAts.payInfoCode = tmpRespCode;
                        if ("0+0000".equals(retAts.transState)) {
                            retAts.respInfo = "提交处理成功，结果待查询！";
                        } else {
                            retAts.respInfo = tmpRetInfo;
                        }
                    }
                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else if (tmpStrRet[0].equals(ConnState.FAIL)) {
                retAts.respCode = "0";
                retAts.respInfo = tmpStrRet[1];
                retAts.reqSeqID = p_reqBat.reqBatchNO;
                retAts.transState = PaymentState.FAIL_Batch;
                retAts.payInfoCode = tmpStrRet[0];
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }

        } catch (Exception e) {
            retAts.respCode = "-1";
            retAts.respInfo = e.toString();
            WriteBankLogLn(e);
        }
        if (retAts.respCode.equals("-1") && StringHelper.isNullOrEmpty(retAts.reqSeqID)) {
            retAts.reqSeqID = p_reqBat.reqBatchNO;
        }
        retAts.writeRetToATS();
        return retAts;
    }

    /**
     * 批量代收付状态查询
     *
     * @param p_reqQryBat
     * @return
     * @throws Exception
     */
    public RetAtsQryBatPayRec d_QryBatPayRec(ReqQryBatPayRec p_reqQryBat) throws Exception {
        RetAtsQryBatPayRec retAts = new RetAtsQryBatPayRec(p_reqQryBat.reqHeader);
        retAts.reqSeqID = p_reqQryBat.reqBatchNO;

        if ("198802s".equals(p_reqQryBat.reqHeader.commandCode)) {
            boolean enableSalaryDecrypt = getBankConfig().getValueAsBool("enableSalaryDecrypt");
            if (enableSalaryDecrypt) {
                isNeedHiddleField = true;
            }
            if ("1".equals(p_reqQryBat.sameBnk)) {
                p_reqQryBat.reqHeader.commandCode = "1988CFRT03";
            } else {
                p_reqQryBat.reqHeader.commandCode = "1988IBBF23";
            }
        }
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();
            ABC02Header bankHeader = new ABC02Header();

            bankHeader.ReqSeqNo = p_reqQryBat.reqHeader.transSeqID;
            String commandCode = p_reqQryBat.reqHeader.commandCode;
            if ("198801N10".equals(commandCode) || "918801N".equals(commandCode) || "198801N".equals(commandCode) || "1988IBBF23".equals(commandCode) || "918804".equals(commandCode) || "1988IBBF01".equals(commandCode) || "1988IBBF02".equals(commandCode) || "198802".equals(commandCode) || "9188IBAF16".equals(commandCode) || "9188IBAF17".equals(commandCode)) {
                bankHeader.CCTransCode = "IBAQ06";
            } else if ("918803".equals(commandCode) || "1988CFRT05".equals(commandCode) || "1988CFRT03".equals(commandCode)) {
                bankHeader.CCTransCode = "CQLT73";
            } else if ("9188C410".equals(commandCode)) {
                bankHeader.CCTransCode = "1972";
            } else if ("928801".equals(commandCode) || "920801".equals(commandCode)) {
                bankHeader.CCTransCode = "CQLA76";
            } else {
                bankHeader.CCTransCode = "IBAQ01";
            }
            writeCommonHeader(xmlWriter, bankHeader);
//            if (!"CQLA76".equals(commandCode)) {
            if (!"CQLA76".equals(bankHeader.CCTransCode)) {
                xmlWriter.writeStartElement("Cmp");
                if (isNeedHiddleField) {
                    xmlWriter.writeElementString("FileEncryptFlag", "1");
                } else {
                    xmlWriter.writeElementString("FileEncryptFlag", "0");
                }
                xmlWriter.writeEndElement();
            }
            xmlWriter.writeStartElement("Corp");
            xmlWriter.writeElementString("CustomNo", p_reqQryBat.reqBatchNO);
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(), p_reqQryBat.reqHeader.transType, p_reqQryBat.reqHeader.commandCode);

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {

                    String tmpTransCode = tmpBkReader.getFirstText("/ap/TransCode");
                    if ("".equals(tmpTransCode) || tmpTransCode == null) {
                        tmpTransCode = tmpBkReader.getFirstText("/ap/CCTransCode");
                    }
                    String tmpTransType = tmpBkReader.getFirstText("/ap/TransType");
                    String tmpFileFlag = tmpBkReader.getFirstText("/ap/FileFlag");
                    String tmpRespCode = tmpBkReader.getFirstText("/ap/RespCode");
                    String tmpRetInfo = tmpBkReader.getFirstText("/ap/RespInfo");
                    String tmpRetRxtInfo = tmpBkReader.getFirstText("/ap/RxtInfo");
                    String tmpRespSource = tmpBkReader.getFirstText("/ap/RespSource");

                    if (tmpRespSource != null && "0".equals(tmpRespSource) && "0000".equals(tmpRespCode)) {
                        retAts.respCode = "0";
                        if ("CQLT73".equals(tmpTransCode) || "CQLA76".equals(tmpTransCode)) {
                            // E -处理失败，
                            // P -尚未处理，
                            // S -处理成功
                            // retAts.transState =
                            // "0+"+tmpBkReader.getFirstText("/ap/Tulip/批处理/处理状态");
                            retAts.transState = tmpBkReader.getFirstText("/ap/Tulip/批处理/主机返回码");
                            // 3．如果FileFl3．如果FileFlag为0，则表示没有文件，具体原因见RxtInfo
                            // 、Tulip/批处理/描述信息中的文字描述ag为0，则表示没有文件，具体原因见RxtInfo、Tulip/批处理/描述信息中的文字描述
                            retAts.respInfo = tmpBkReader.getFirstText("/ap/Tulip/批处理/描述信息");
                            retAts.payInfoCode = tmpBkReader.getFirstText("/ap/Tulip/批处理/主机返回码");
//							if ("1".equals(tmpFileFlag) && !"0000".equals(retAts.transState)) {
//								tmpFileFlag = "0";
//							}
                            //华润专用,当不返回文件时，直接把RetInfo的信息展示给用户
                            if (!"1".equals(tmpBkReader.getFirstText("/ap/FileFlag")) && isNeedHiddleField) {
                                retAts.respCode = "-1";
                                retAts.respInfo = tmpRetRxtInfo;
                                retAts.writeAfterDetail();
                                return retAts;
                            }
                        } else {
                            String batchStatus = tmpBkReader.getFirstText("/ap/BatchPlat/批处理/状态");
                            if (!"5".equalsIgnoreCase(batchStatus)) {
                                retAts.transState = batchStatus;
                                if (tmpBkReader.getFirstText("/ap/BatchPlat/批处理/子状态") != null) {
                                    //不是华润渠道就在交易结果中添加子状态
                                    if (!isNeedHiddleField) {
                                        retAts.transState = retAts.transState + "+" + tmpBkReader.getFirstText("/ap/BatchPlat/批处理/子状态");
                                    }
                                }
                                retAts.respInfo = tmpRetRxtInfo;
                                retAts.payInfoCode = retAts.respInfo;
                                retAts.writeAfterDetail();
                                return retAts;
                            }
                        }
                        if ("1".equals(tmpFileFlag)) {
                            if ("IBAF16".equals(tmpTransType) || "CQLA76".equals(tmpTransCode)) {
                                String tmpBatchFileName = tmpBkReader.getFirstText("/ap/Cmp/BatchFileName");
                                String tmpFilePath = "";
//                                String tmpQryPayFolder = bankFront.bkPayFolder;
                                String tmpQryRecFolder = bankFront.bkQryRecFolder;
                                boolean isDirCut = getBankConfig().getValueAsBool("dirCutFlag");
                                if (isDirCut) {
//                                    tmpQryPayFolder = bankFront.bkQryPayFolder;
                                    tmpQryRecFolder = bankFront.bkQryRecFolder;
                                }
                                tmpFilePath = StringHelper.trimEnd(tmpQryRecFolder, File.separator) + File.separator + tmpBatchFileName;
                                this.WriteBankLogLn("读取前置数据文件：" + tmpFilePath);
                                String tmpResultData = "";
                                if ("dspfileserver".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                                    WriteBankLogLn("accessBkFileMode：" + bankFront.accessBkFileMode);
                                    DspClient dspClient = new DspClient(bankFront.dspFileServer);
                                    dspClient.charsetName = getCharset();
                                    TcpReceiver tcpRec = dspClient.getFile(FileType.bankFile, tmpFilePath);
                                    if (tcpRec.isCompleted) {
                                        if (tcpRec.isRtnBinaryFile) {
                                            tmpResultData = new String(tcpRec.recBytes, tcpRec.contentStartIndex, tcpRec.contentLength, tcpRec.charsetName);
                                        } else {
                                            String errMsg = new String(tcpRec.recBytes, tcpRec.contentStartIndex, tcpRec.contentLength, tcpRec.charsetName);
                                            WriteBankLogLn("从远程DSP（" + bankFront.dspFileServer + "）获取前置文件未完成：" + errMsg);
                                        }
                                    } else {
                                        this.WriteBankLogLn("从远程DSP（" + bankFront.dspFileServer + "）获取前置文件未完成：" + tcpRec.getConnMsg());
                                    }
                                } else if ("smb".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                                    String downloadFilePath = trimEndFileSp(bankFront.bkQryRecFolder) + File.separator;//共享目录路径
                                    String p_FileFullPath = getTempFileDirectory() + File.separator + Format.Time6Format.format(new Date());//下载到的本地路径
                                    StringBuilder p_sb = new StringBuilder();
                                    if (SmbFileUtil.smbDownloadInChar(p_FileFullPath, downloadFilePath, tmpBatchFileName, p_sb) == 1) {
                                        WriteBankLogLn("文件下载成功，位于：" + p_FileFullPath);
                                        p_FileFullPath = p_FileFullPath + File.separator + tmpBatchFileName;
                                        tmpResultData = FileHelper.readFileText(p_FileFullPath, getCharset());
                                    } else {
                                        retAts.writeRespCode("-1");
                                        retAts.writeRespInfo(p_sb.toString());
                                        retAts.writeEnd();
                                        return retAts;
                                    }
                                } else {
                                    tmpResultData = FileHelper.readFileText(tmpFilePath, getCharset());
                                }
                                if (isNeedHiddleField) {
                                    String cusPriKeyPath = getBankConfig().getValueAsString("CusPriKeyPath");
                                    PrivateKey privateKey = RsaHelper.decodePrivateKeyFromFile(cusPriKeyPath);
                                    byte[] encryptedDate = new BASE64Decoder().decodeBuffer(tmpResultData);
                                    byte[] decryptedDate = RsaHelper.decryptData(encryptedDate, privateKey);
                                    tmpResultData = new String(decryptedDate, "GBK");
                                }
                                WriteBankLogLn("银行查询文件返回内容为：" + tmpResultData);
                                StringBuilder tmpSbLog = new StringBuilder();
                                ArrayList<String[]> tmpArrayList = new ArrayList<String[]>();
                                if ("IBAF16".equals(tmpTransType)) {
                                    tmpArrayList = MFSHelper.getTextArray(tmpResultData, "|_|", 16, -1, tmpSbLog);
                                    if (tmpSbLog.length() == 0) {
                                        for (int i = 0; i < tmpArrayList.size(); i++) {
                                            String[] tmpEachArr = tmpArrayList.get(i);
                                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                            tmpRetDetail.detailNo = tmpEachArr[0];
                                            tmpRetDetail.recName = tmpEachArr[1];
                                            tmpRetDetail.recAct = tmpEachArr[2];
                                            tmpRetDetail.payAmount = tmpEachArr[5];
                                            tmpRetDetail.postScript = tmpEachArr[6];
                                            //0+    0 待发送/1未知/E 不合法/4 成功/5 失败/9 未知
                                            tmpRetDetail.result = tmpRespSource + "+" + tmpEachArr[13];
                                            tmpRetDetail.payInfoCode = tmpEachArr[14];
                                            tmpRetDetail.memo = tmpEachArr[15];
                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    } else {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = tmpSbLog.toString();
                                    }
                                } else if ("CQLA76".equals(tmpTransCode)) {
                                    tmpArrayList = MFSHelper.getTextArray(tmpResultData, "|", 9, -1, tmpSbLog);
                                    if (tmpSbLog.length() == 0) {
                                        for (int i = 0; i < tmpArrayList.size(); i++) {
                                            String[] tmpEachArr = tmpArrayList.get(i);
                                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                            tmpRetDetail.detailNo = tmpEachArr[0];
                                            tmpRetDetail.recName = tmpEachArr[1];
                                            tmpRetDetail.recAct = tmpEachArr[2];
                                            tmpRetDetail.payAmount = tmpEachArr[3];
                                            tmpRetDetail.postScript = tmpEachArr[4];
                                            tmpRetDetail.result = tmpRespCode + "+" + tmpEachArr[5];
                                            tmpRetDetail.successAmount = tmpEachArr[6];
                                            tmpRetDetail.payInfoCode = tmpRetDetail.result;
                                            tmpRetDetail.memo = tmpEachArr[7];
                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    } else {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = tmpSbLog.toString();
                                    }
                                }

                            }
                            // 没有transType
                            else if ("IBAF04".equals(tmpTransType) || "IBAF05".equals(tmpTransType) || "IBBF23".equals(tmpTransType) || "CQLT73".equals(tmpTransCode) || "IBBF01".equals(tmpTransType) || "IBBF02".equals(tmpTransType) || "198802".equals(tmpTransType) || "9188C410".equals(tmpTransType) || "9188IBAF16".equals(tmpTransType) || "CFRB16".equals(tmpTransType) || "CFRB17".equals(tmpTransType) || "1972".equals(tmpTransCode) || "IBAF17".equals(tmpTransType)) {
                                String tmpBatchFileName = tmpBkReader.getFirstText("/ap/Cmp/BatchFileName");
                                String tmpFilePath = "";
                                String tmpQryPayFolder = bankFront.bkPayFolder;
                                String tmpQryRecFolder = bankFront.bkRecFolder;
                                boolean isDirCut = getBankConfig().getValueAsBool("dirCutFlag");
                                if (isDirCut) {
                                    tmpQryPayFolder = bankFront.bkQryPayFolder;
                                    tmpQryRecFolder = bankFront.bkQryRecFolder;
                                }
                                if (p_reqQryBat.reqHeader.transType.equals("1998")) {
                                    tmpFilePath = StringHelper.trimEnd(tmpQryPayFolder, File.separator) + File.separator + tmpBatchFileName;
                                } else if (p_reqQryBat.reqHeader.transType.equals("9198")) {
                                    tmpFilePath = StringHelper.trimEnd(tmpQryRecFolder, File.separator) + File.separator + tmpBatchFileName;
                                }
                                this.WriteBankLogLn("读取前置数据文件：" + tmpFilePath);
                                String tmpResultData = "";
                                if ("dspfileserver".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                                    WriteBankLogLn("accessBkFileMode：" + bankFront.accessBkFileMode);
                                    DspClient dspClient = new DspClient(bankFront.dspFileServer);
                                    dspClient.charsetName = getCharset();
                                    TcpReceiver tcpRec = dspClient.getFile(FileType.bankFile, tmpFilePath);
                                    if (tcpRec.isCompleted) {
                                        if (tcpRec.isRtnBinaryFile) {
                                            tmpResultData = new String(tcpRec.recBytes, tcpRec.contentStartIndex, tcpRec.contentLength, tcpRec.charsetName);
                                        } else {
                                            String errMsg = new String(tcpRec.recBytes, tcpRec.contentStartIndex, tcpRec.contentLength, tcpRec.charsetName);
                                            WriteBankLogLn("从远程DSP（" + bankFront.dspFileServer + "）获取前置文件未完成：" + errMsg);
                                        }
                                    } else {
                                        this.WriteBankLogLn("从远程DSP（" + bankFront.dspFileServer + "）获取前置文件未完成：" + tcpRec.getConnMsg());
                                    }
                                }
                                if ("ftp".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                                    FtpSet ftpSet = bankFront.getFtpSet("batPay");
                                    String downFilePath = StringHelper.trimEndFileSp(ftpSet.downDir) + "/" + tmpBatchFileName;
                                    FtpPortClient ftpPtClient = new FtpPortClient(ftpSet);
                                    ftpPtClient.connect();
                                    FtpReceiver ftpReceiver = ftpPtClient.downloadFile(downFilePath, tmpFilePath);
                                    if (ftpReceiver.isCompleted) {
                                        WriteBankLogLn("成功下载批量支付结果文件:" + tmpBatchFileName + "至" + tmpFilePath);
                                    } else {
                                        WriteBankLogLn(tmpBatchFileName + "批量支付结果文件下载失败:" + ftpReceiver.connMsg + "+" + ftpReceiver.connState);
                                        retAts.respCode = "-1";
                                        retAts.respInfo = tmpBatchFileName + "批量支付结果文件下载失败:" + ftpReceiver.connMsg + "+" + ftpReceiver.connState;
                                        retAts.writeAfterDetail();
                                        return retAts;
                                    }
                                    tmpResultData = FileHelper.readFileText(tmpFilePath, getCharset());
                                    WriteBankLogLn("批量支付结果文件内容为:" + tmpResultData);

                                } else if ("smb".equals(this.bankFront.getAccessBkFileModeLowerCase())) {
                                    String downloadFilePath = trimEndFileSp(bankFront.bkQryRecFolder) + File.separator;//共享目录路径
                                    String p_FileFullPath = getTempFileDirectory() + File.separator + Format.Time6Format.format(new Date());//下载到的本地路径
                                    StringBuilder p_sb = new StringBuilder();
                                    if (SmbFileUtil.smbDownloadInChar(p_FileFullPath, downloadFilePath, tmpBatchFileName, p_sb) == 1) {
                                        WriteBankLogLn("文件下载成功，位于：" + p_FileFullPath);
                                        p_FileFullPath = p_FileFullPath + File.separator + tmpBatchFileName;
                                        tmpResultData = FileHelper.readFileText(p_FileFullPath, getCharset());
                                    } else {
                                        retAts.writeRespCode("-1");
                                        retAts.writeRespInfo(p_sb.toString());
                                        retAts.writeEnd();
                                        return retAts;
                                    }
                                } else {
                                    tmpResultData = FileHelper.readFileText(tmpFilePath, getCharset());
                                }
                                if (isNeedHiddleField) {
                                    String cusPriKeyPath = getBankConfig().getValueAsString("CusPriKeyPath");
                                    PrivateKey privateKey = RsaHelper.decodePrivateKeyFromFile(cusPriKeyPath);
                                    byte[] encryptedDate = new BASE64Decoder().decodeBuffer(tmpResultData);
                                    byte[] decryptedDate = RsaHelper.decryptData(encryptedDate, privateKey);
                                    tmpResultData = new String(decryptedDate, "GBK");
                                }
                                WriteBankLogLn("银行查询文件返回内容为：" + tmpResultData);
                                StringBuilder tmpSbLog = new StringBuilder();
                                if ("IBBF23".equals(tmpTransType)) {
                                    String tmpLocalMarkPath = getTempFileDirectory(p_reqQryBat.getBgnDateTime()) + File.separator + p_reqQryBat.reqBatchNO + ".txt";
                                    WriteBankLogLn2("批次流水映射文件路径: " + tmpLocalMarkPath);
                                    File localMarkFile = new File(tmpLocalMarkPath);
                                    if (!localMarkFile.exists()) {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = "批次流水映射文件未生成,未发生交易!";
                                        retAts.writeAfterDetail();
                                        return retAts;
                                    }
                                    String tmpSeqIDData = FileHelper.readToString(localMarkFile, getCharset());
                                    WriteBankLogLn2("本地流水映射文件内容:" + tmpSeqIDData);
                                    if (StringHelper.isNullOrEmpty(tmpSeqIDData)) {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = "批次流水映射文件内容为空,未发生交易!";
                                        retAts.writeAfterDetail();
                                        return retAts;
                                    }
                                    String[] tmpSeqIDArr = tmpSeqIDData.split("\r\n");
                                    int fieldNum = Integer.valueOf(tmpBkReader.getFirstText("/ap/Cme/FieldNum"));
                                    ArrayList<String[]> tmpArrayList = MFSHelper.getTextArray(tmpResultData, "|_|", fieldNum, -1, tmpSbLog);
                                    if (tmpSbLog.length() == 0) {
                                        WriteBankLogLn("返回的记录条数：" + tmpArrayList.size());

                                        for (int i = 0; i < tmpArrayList.size(); i++) {
//											交易序号0|_|客户请求序号1|_|系统流水号2|_|交易日期3|_|交易类型4|_|交易金额5|_|流水状态6|_|失败原因7|_|对方账号8|_|对方户名9|_|用途10|_|

                                            String[] tmpEachArr = tmpArrayList.get(i);
                                            WriteBankLogLn("返回的记录长度：" + tmpEachArr.length);
                                            WriteBankLogLn("返回的记录内容：" + Arrays.toString(tmpEachArr));
                                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                            tmpRetDetail.recAct = tmpEachArr[8];
                                            tmpRetDetail.recName = tmpEachArr[9];
                                            if (!isNeedHiddleField) {
                                                tmpRetDetail.payAmount = tmpEachArr[5];
                                            }
                                            tmpRetDetail.postScript = tmpSeqIDArr[i].split(",")[1];
                                            tmpRetDetail.result = "0+" + tmpEachArr[6];
                                            if (getBankConfig().getValueAsBool("BatPayRecMoreInfo")) {
                                                tmpRetDetail.payInfoCode = "0+" + tmpEachArr[6] + "+" + tmpEachArr[7];
                                            } else {
                                                tmpRetDetail.payInfoCode = "0+" + tmpEachArr[6];
                                            }
                                            tmpRetDetail.memo = tmpEachArr[7];

                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    } else {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = tmpSbLog.toString();
                                    }
                                } else if ("IBBF01".equals(tmpTransType) || "IBBF02".equals(tmpTransType)) {
                                    String tmpLocalMarkPath = getTempFileDirectory(p_reqQryBat.getBgnDateTime()) + File.separator + p_reqQryBat.reqBatchNO + ".txt";
                                    WriteBankLogLn2("批次流水映射文件路径: " + tmpLocalMarkPath);
                                    File localMarkFile = new File(tmpLocalMarkPath);
                                    if (!localMarkFile.exists()) {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = "批次流水映射文件未生成,未发生交易!";
                                        retAts.writeAfterDetail();
                                        return retAts;
                                    }
                                    String tmpSeqIDData = FileHelper.readToString(localMarkFile, getCharset());
                                    WriteBankLogLn2("本地流水映射文件内容:" + tmpSeqIDData);
                                    if (StringHelper.isNullOrEmpty(tmpSeqIDData)) {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = "批次流水映射文件内容为空,未发生交易!";
                                        retAts.writeAfterDetail();
                                        return retAts;
                                    }
                                    String[] tmpSeqIDArr = tmpSeqIDData.split("\r\n");

                                    if ("IBBF02".equals(tmpTransType)) {
                                        ArrayList<String[]> tmpArrayList = MFSHelper.getTextArray(tmpResultData, "|_|", 14, -1, tmpSbLog);
                                        if (tmpSbLog.length() == 0) {
                                            for (int i = 0; i < tmpArrayList.size(); i++) {
                                                String[] tmpEachArr = tmpArrayList.get(i);
                                                RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                                tmpRetDetail.recAct = tmpEachArr[4];
                                                tmpRetDetail.recName = tmpEachArr[6];
                                                tmpRetDetail.payAmount = tmpEachArr[8];
                                                tmpRetDetail.postScript = tmpSeqIDArr[i].split(",")[1];
                                                tmpRetDetail.result = "0+" + tmpEachArr[11];
                                                tmpRetDetail.payInfoCode = tmpEachArr[12];
                                                tmpRetDetail.memo = tmpEachArr[13];

                                                retAts.writeRetDetail(tmpRetDetail);
                                            }
                                        } else {
                                            retAts.respCode = "-1";
                                            retAts.respInfo = tmpSbLog.toString();
                                        }
                                    } else {
                                        ArrayList<String[]> tmpArrayList = MFSHelper.getTextArray(tmpResultData, "|_|", 8, -1, tmpSbLog);
                                        if (tmpSbLog.length() == 0) {
                                            for (int i = 0; i < tmpArrayList.size(); i++) {
                                                String[] tmpEachArr = tmpArrayList.get(i);
                                                RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                                tmpRetDetail.recAct = tmpEachArr[2];
                                                tmpRetDetail.recName = tmpEachArr[1];
                                                tmpRetDetail.payAmount = tmpEachArr[3];
                                                tmpRetDetail.postScript = tmpSeqIDArr[i].split(",")[1];
                                                tmpRetDetail.result = "0+" + tmpEachArr[5];
                                                tmpRetDetail.payInfoCode = tmpEachArr[6];
                                                tmpRetDetail.memo = tmpEachArr[7];

                                                retAts.writeRetDetail(tmpRetDetail);
                                            }
                                        } else {
                                            retAts.respCode = "-1";
                                            retAts.respInfo = tmpSbLog.toString();
                                        }
                                    }
                                } else if ("CQLT73".equals(tmpTransCode)) {
                                    ArrayList<String[]> tmpArrayList = MFSHelper.getTextArray(tmpResultData, "|", 7, -1, tmpSbLog);
                                    if (tmpSbLog.length() == 0) {
                                        for (int i = 0; i < tmpArrayList.size(); i++) {
                                            String[] tmpEachArr = tmpArrayList.get(i);
                                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                            if ("1998".equals(p_reqQryBat.reqHeader.transType) || "9198".equals(p_reqQryBat.reqHeader.transType)) {
                                                if ("1998".equals(p_reqQryBat.reqHeader.transType)) {
                                                    tmpRetDetail.recAct = tmpEachArr[2];
                                                    tmpRetDetail.recName = tmpEachArr[3];
                                                } else {
                                                    tmpRetDetail.payAct = tmpEachArr[2];
                                                    tmpRetDetail.payName = tmpEachArr[3];
                                                }

                                                if (!isNeedHiddleField) {
                                                    tmpRetDetail.payAmount = tmpEachArr[4];
                                                }
                                                tmpRetDetail.postScript = tmpEachArr[5];

                                                // 0+E 0+P 0+S
                                                // E -处理失败，P -尚未处理，S -处理成功
                                                tmpRetDetail.result = "0+" + tmpEachArr[1];
                                                if ("E".equals(tmpEachArr[1])) {
                                                    tmpRetDetail.payInfoCode = tmpEachArr[6];
                                                } else {
                                                    tmpRetDetail.payInfoCode = tmpRetDetail.result;
                                                }
												/*tmpRetDetail.memo = tmpEachArr[6];
												// 获取[62284500100447231171]的JJ码失败,
												// 省市代码=[11] ,RTO3957
												if (("".equals(tmpEachArr[6]) || tmpEachArr[6] == null) && "0+S".equals(tmpRetDetail.result)) {
													tmpRetDetail.memo = "支付成功";
												}*/
                                            }
                                            tmpRetDetail.memo = tmpEachArr[6];
                                            if (("".equals(tmpEachArr[6]) || tmpEachArr[6] == null) && "0+S".equals(tmpRetDetail.result)) {
                                                tmpRetDetail.memo = "支付成功";
                                            }
                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    } else {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = tmpSbLog.toString();
                                    }
                                } else if ("1972".equals(tmpTransCode)) {
                                    retAts.respInfo = tmpRetInfo;
                                    retAts.transState = tmpBkReader.getFirstText("/ap/Tulip/批处理/主机返回码");
                                    String tmpLocalMarkPath = getTempFileDirectory(p_reqQryBat.getBgnDateTime()) + File.separator + p_reqQryBat.reqBatchNO + ".txt";
                                    WriteBankLogLn2("批次流水映射文件路径: " + tmpLocalMarkPath);
                                    File localMarkFile = new File(tmpLocalMarkPath);
                                    if (!localMarkFile.exists()) {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = "批次流水映射文件未生成,未发生交易!";
                                        retAts.writeAfterDetail();
                                        return retAts;
                                    }
                                    String tmpSeqIDData = FileHelper.readToString(localMarkFile, getCharset());
                                    WriteBankLogLn2("本地流水映射文件内容:" + tmpSeqIDData);
                                    if (StringHelper.isNullOrEmpty(tmpSeqIDData)) {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = "批次流水映射文件内容为空,未发生交易!";
                                        retAts.writeAfterDetail();
                                        return retAts;
                                    }
                                    String[] tmpSeqIDArr = tmpResultData.split("\r\n");
                                    if (tmpSbLog.length() == 0) {
                                        WriteBankLogLn("返回的记录条数：" + tmpSeqIDArr.length);

                                        for (int i = 0; i < tmpSeqIDArr.length; i++) {
                                            byte[] tmpSeqId = tmpSeqIDArr[i].getBytes(getCharset());
                                            //客户编号C20 客户姓名C32 客户账号C30 交易金额N18.2 摘要C30 处理结果C1 ABIS交易码（或返回码）C6
                                            byte[] tmpEachArr = new byte[tmpSeqId.length];
                                            System.arraycopy(tmpSeqId, 0, tmpEachArr, 0, tmpSeqId.length);
                                            WriteBankLogLn("返回的记录长度：" + tmpEachArr.length);
                                            WriteBankLogLn("返回的记录内容：" + tmpEachArr);
                                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                            tmpRetDetail.payAct = new String(tmpEachArr, 52, 30, "GBK").trim();
                                            tmpRetDetail.payName = new String(tmpEachArr, 20, 32, "GBK").trim();
                                            if (!isNeedHiddleField) {
                                                tmpRetDetail.payAmount = new String(tmpEachArr, 82, 18, "GBK").trim();
                                            }
                                            tmpRetDetail.result = "0+" + new String(tmpEachArr, 130, 1, "GBK").trim();
                                            if ("E".equals(new String(tmpEachArr, 130, 1, "GBK").trim())) {
                                                tmpRetDetail.payInfoCode = new String(tmpEachArr, 130, 1, "GBK").trim();
                                            } else {
                                                tmpRetDetail.payInfoCode = tmpRetDetail.result;
                                            }
                                            tmpRetDetail.postScript = new String(tmpEachArr, 0, 20, "GBK").trim();
                                            tmpRetDetail.memo = new String(tmpEachArr, 100, 30, "GBK").trim();
                                            retAts.payInfoCode = tmpRetDetail.payInfoCode;
                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    } else {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = tmpSbLog.toString();
                                    }
                                } else {
                                    ArrayList<String[]> tmpArrayList = MFSHelper.getTextArray(tmpResultData, "|_|", 16, -1, tmpSbLog);
                                    if (tmpSbLog.length() == 0) {
                                        for (int i = 0; i < tmpArrayList.size(); i++) {
                                            String[] tmpEachArr = tmpArrayList.get(i);
                                            RetAtsBatPayRecDetail tmpRetDetail = new RetAtsBatPayRecDetail();
                                            if (p_reqQryBat.reqHeader.transType.equals("1998")) {
                                                tmpRetDetail.recAct = tmpEachArr[2];
                                                tmpRetDetail.recName = tmpEachArr[1];
                                            } else if (p_reqQryBat.reqHeader.transType.equals("9198")) {
                                                tmpRetDetail.payAct = tmpEachArr[2];
                                                tmpRetDetail.payName = tmpEachArr[1];
                                            }
                                            tmpRetDetail.payAmount = tmpEachArr[5];
                                            tmpRetDetail.postScript = tmpEachArr[6];
                                            tmpRetDetail.result = "0+" + tmpEachArr[13];
                                            if (getBankConfig().getValueAsBool("BatPayRecMoreInfo")) {
                                                tmpRetDetail.payInfoCode = "0+" + tmpEachArr[13] + "+" + tmpEachArr[14];
                                            } else {
                                                tmpRetDetail.payInfoCode = "0+" + tmpEachArr[13];
                                            }
                                            tmpRetDetail.memo = tmpEachArr[13] + "-" + tmpEachArr[14] + "-" + tmpEachArr[15];
                                            retAts.writeRetDetail(tmpRetDetail);
                                        }
                                    } else {
                                        retAts.respCode = "-1";
                                        retAts.respInfo = tmpSbLog.toString();
                                    }
                                }
                            } else {
                                retAts.respCode = "-1";
                                retAts.respInfo = "不支持的结果文件格式：" + tmpTransType;
                            }
                        } else if ("0".equals(tmpFileFlag)) {
                            retAts.respCode = "-1";
                            retAts.respInfo = "处理中，没有结果文件" + StringHelper.joinNotEmpty("+", tmpBkReader.getFirstText("/ap/Tulip/批处理/描述信息"), tmpRetRxtInfo);
                        }
                    } else if ("CQLT73".equals(tmpTransCode) && "3".equals(tmpRespSource)) {
                        String cmeRecordNum = tmpBkReader.getFirstText("/ap/Cme/RecordNum");
                        if ("0".equals(cmeRecordNum) && "1028".equals(tmpRespCode) && tmpRetInfo.contains("无满足条件的记录") && tmpRetRxtInfo.contains("查询无数据")) {
                            if (canBeNotExist(p_reqQryBat, retAts, 45)) {
                                retAts.respCode = "0";
                                retAts.reqSeqID = p_reqQryBat.reqBatchNO;
                                retAts.transState = tmpRespCode + "+" + tmpRetInfo;
                                retAts.payInfoCode = tmpRespCode;
                                retAts.respInfo = tmpRetInfo;
                                retAts.writeAfterDetail();
                                return retAts;
                            } else {
                                retAts.writeAfterDetail();
                                return retAts;
                            }
                        }
                    } else {
                        retAts.respCode = "-1";
                        if (!StringHelper.isNullOrEmpty(tmpRetRxtInfo)) {
                            retAts.respInfo = tmpRetRxtInfo;
                        } else {
                            retAts.respInfo = tmpRetInfo;
                        }

                        if ("1015".equals(tmpRespCode) || "1028".equals(tmpRespCode)) { // 无此交易
                            if (canBeNotExist(p_reqQryBat, retAts, 45)) {
                                retAts.respCode = "0";
                                retAts.reqSeqID = p_reqQryBat.reqBatchNO;
                                retAts.transState = tmpRespCode + "+" + tmpRetInfo;
                                retAts.payInfoCode = tmpRespCode;
                                retAts.writeAfterDetail();
                                return retAts;
                            }
                        }
                    }

                } else {
                    retAts.respCode = "-1";
                    retAts.respInfo = tmpSbMsg.toString();
                }
            } else {
                retAts.respCode = "-1";
                retAts.respInfo = tmpStrRet[1];
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeAfterDetail();
        return retAts;
    }
}